﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class admin_edcountonline : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from CountOnline where nameUser = 'ActualUser'", cn);
        mySqlSelect.CommandType = CommandType.Text;
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                cou.Text = rd.GetSqlInt64(1).Value.ToString();
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void update_Click(object sender, EventArgs e)
    {
        lbInfo.Text = "";
        if (count.Text != "")
        {
            try
            {
                connection con = new connection();
                SqlConnection cn = con.connect();
                SqlCommand mySqlSelect = new SqlCommand("update CountOnline set total = @total where nameUser = 'FakeUser'", cn);
                mySqlSelect.CommandType = CommandType.Text;
                SqlParameter total = new SqlParameter("@total", SqlDbType.BigInt);
                total.Value = Int64.Parse(count.Text);
                mySqlSelect.Parameters.Add(total);
                mySqlSelect.ExecuteNonQuery();
                cn.Close();
                lbInfo.Visible = true;
                lbInfo.Text = "Bạn đã cập nhật thành công";
            }
            catch (Exception ex)
            {
                lbInfo.Text = "Quá trình nhập dữ liệu có lỗi";
            }
        }
        else
        {
            lbInfo.Text = "Mời bạn nhập số liệu";
        }
    }
}